/**
 * 数据字典服务
 */
(function (app, ng) {
    'use strict';
    app.service('DictService', Service);
    /**
     * 依赖项
     * @type {[string,string]}
     */
    Service.$inject = ['$q', '$http', '$resource'];

    function Service($q, $http, $resource) {
        var service = this;
        // 当前模块的默认路径
        service.getPath = function (s) {
            return ISS.getPath(s ? 'dictionary/' + s : 'dictionary');
        };
        this.resource = $resource('dictionary/:id', {}, {
            update: { // 获取巡查地址
                url: 'dictionary',
                method: 'PUT',
                headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                contentType: 'form'
            }
        });
        /**
         * 获取表格参数
         * @returns {{height: number, url, dataType: string, colModel: *[], usepager: boolean, rp: number, pageMode: string, showcheckbox: boolean}}
         */
        this.getGirdOptions = function () {
            return {
                // height: 450,
                url: service.getPath('list'),
                dataType: 'json',
                useRp: false,
                colModel: [
                    {
                        display: 'dict.desc',
                        name: 'content',
                        width: 180
                    },
                    {
                        display: 'dict.code',
                        name: 'code',
                        width: 240
                    },
                    //{
                    //    display: 'common.createTime',
                    //    name: 'createTime',
                    //    width: 180
                    //},
                    {
                        display: 'common.operation',
                        name: 'id',
                        width: 180,
                        process: function (id) {
                            var html = [];
                            //iss-permission="EMS_DICTIONARY_7002"
                            html.push('<button class="btn m-r-xs btn-xs btn-primary" ng-click="vm.updateDict(\'' + id + '\')">');
                            html.push('  <i class="fa fa-pencil"></i>');
                            html.push('  <span translate="common.edit"></span>');
                            html.push('</button>');
                            html.push('<button class="btn m-r-xs btn-xs btn-danger" ng-click="vm.delDict(\'' + id + '\')">');
                            html.push('  <i class="fa fa-trash-o"></i>');
                            html.push('  <span translate="common.del"></span>');
                            html.push('</button>');
                            return html.join('');
                        }
                    }
                ],
                usepager: true,
                rp: 1000,
                pageMode: 'jpa',
                showcheckbox: false
            };
        };
        /**
         * 查询全部数据字典类型
         * @returns promise
         */
        this.getCategories = function (lang) {
            return $http.post(this.getPath('categories/' + lang)).then(function (resp) {
                return resp.data;
            });
        };
        /**
         * 查询一个数据字典
         * @param id 主键
         * @returns promise
         */
        this.getDict = function (id) {
            return $http.get(
                service.getPath(ISS.lang + '/' + id)
            ).then(function (resp) {
                return resp.data;
            });
        };
        /**
         * 新增数据字典
         * @param data 数据字典和国际化信息
         * @returns promise
         */
        this.addDict = function (dict, i18n) {
            var data = merginDictAndI18n(dict, i18n);
            return $http.post(
                service.getPath(),
                data
            ).then(function (resp) {
                return resp.data;
            });
        };
        /**
         * 更新数据字典
         * @param dict
         * @param i18n
         * @returns promise
         */
        this.update = function (dict, i18n) {
            var model = angular.extend({}, i18n, dict);
            return this.defer(model, service.resource.update);
            // return $http.put(
            //     service.getPath('update'),
            //     model,
            //     {headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}}
            // ).then(function (resp) {
            //     return resp.data;
            // });
        };
        /**
         * 更新数据字典
         * @returns promise
         */
        this.updateDictJs = function () {
            return $http.post(
                service.getPath('update/js')
            ).then(function (resp) {
                return resp.data;
            });
        };
        /**
         * 删除数据字典
         * @param id 主键
         * @returns promise
         */
        this.deleteDict = function (id) {
            return $http.delete(
                service.getPath(id)
            ).then(function (resp) {
                return resp.data;
            });
        };
        this.deleteDictCategory = function (datadictCategoryId) {
            return $http.post(
                ISS.getPath('/datadict/dels/' + datadictCategoryId)
            ).then(function (resp) {
                return resp.data;
            });
        };
        this.updateDictCategory = function (datadictCategoryId, datadictCategoryName) {
            return $http.post(
                ISS.getPath('/datadict/updates/' + datadictCategoryId + '/' + datadictCategoryName)
            ).then(function (resp) {
                return resp.data;
            });
        };

        this.defer = function (model, request) { // 延迟计划
            var defer = $q.defer();
            request && request(model, function (data/*,headers*/) {
                defer.resolve(data);
            }, function (data/*,headers*/) {
                defer.reject(data);
            });
            return defer.promise;
        };

        /**
         * 合并数据字典和国际化
         * @param dict 数据字典
         * @param i18n 国际化信息
         */
        function merginDictAndI18n(dict, i18n) {
            return $.extend({}, dict, i18n);
        }
    }

})(iApp.module(), window.angular);